#!/bin/bash

export GOVC_URL="{{ vc_host }}"
export GOVC_USERNAME="{{ vc_username }}"
export GOVC_PASSWORD='{{ vc_password }}'
export GOVC_INSECURE="1"
DATACENTER="{{ region }}"
FOLDER="kubeoperator"

IFS=$'\n'
for vm in $(govc ls "/$DATACENTER/vm/$FOLDER" | grep {{cluster_name}} | grep {{ inventory_hostname }}); do
  MACHINE_INFO=$(govc vm.info -json -dc=$DATACENTER -vm.ipath="/$vm" -e=true)
  VM_NAME=$(jq -r ' .VirtualMachines[] | .Name' <<< $MACHINE_INFO | awk '{print tolower($0)}')
  VM_UUID=$( jq -r ' .VirtualMachines[] | .Config.Uuid' <<< $MACHINE_INFO | awk '{print toupper($0)}')
  echo "govc vm.change -e=\"disk.enableUUID=1\" -vm=\"/$DATACENTER/vm/$FOLDER/$VM_NAME\""
  govc vm.change -e="disk.enableUUID=1" -vm="/$DATACENTER/vm/$FOLDER/$VM_NAME"
  echo "Patching $VM_NAME with UUID:$VM_UUID"
  kubectl patch node $VM_NAME -p "{\"spec\":{\"providerID\":\"vsphere://$VM_UUID\"}}"
done
